

* * * * * PLOT GRAPHS OF THE TAX VARIATION (1ST STAGE) * * * * *


* Collapse by different regressive tax brackets and make graphs of NTR by group

cd "$mypathRR/Results/"
local b " 60 "
local t " 80 "
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'

cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"


preserve
drop if year>2010


* graphs of the tax variation
cap binscatter avg_ntr year , rd(2005.5 2007.5) by(treat_i) ///
ytitle("Average net-of-tax rate") xtitle(" ") legend( label(1 "control") label(2 "treated") ) ///
savegraph("Fig_7a)-avgntr_Incomers_inctreat_`x'_`y'_uw.pdf") replace

restore

}





*************************************************************************************************

* REGRESSIONS *
// specification with 1 treatment (2006), dropping all obs after 2007

cap rm "$mypathRR/Results/appendixtables/B3_appendixtable.xls"
cap rm "a$mypathRR/Results/appendixtables/B3_appendixtable.txt"

cd "$mypathRR/Results/Resident/results_01-07"

* INCLUDE YEARS UP TO 2007 AND INSTRUMENT 2006 REFORM

local b " 60 60"
local t " 80 95"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'

cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"



preserve
drop if year>2007

collapse (count) Inmovers=persid (mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(year treat_i)
label var Inmovers "Residents"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var year "Year"


gen Period=(year>=2006)
gen DiD=treat_i*Period
gen weight=Inmovers

* for the semi-elasticities: average change in tax for treatment group
drop if DiD==.
sort treat_i year

bys treat_i: egen tax_0=mean(avgtt) if year<2006
bys treat_i: egen tax_1=mean(avgtt) if year>2005 & year<2008
bys treat_i (year): replace tax_0=tax_0[_n-1] if tax_0==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.

gen delta_tax=tax_1-tax_0 if treat_i==1
sum delta_tax
local delta_tax = abs(`r(mean)')
local delta_tax2 = `delta_tax'*100
global delta_tax_2006_`x'_`y' = `delta_tax2'

gen delta_tax_percent = delta_tax/tax_0
sum delta_tax_percent
local delta_tax_percent = abs(`r(mean)')
local delta_tax_percent2 = `delta_tax_percent'*100
global delta_tax_pct_2006_`x'_`y' = `delta_tax_percent2'




*  REDUCED FORM DID REGRESSION IN LEVELS 
display _newline(5)
display "Reduced Form, Control group: `x'-`y'%"

sum Inmovers if year <2006 & year >2000 & treat_i == 1
scalar define meanoutcome = `r(mean)'
global mean_incomers`x'_`y' = meanoutcome

reg Inmovers DiD treat_i  ib2005.year, vce(robust)
scalar define semi_e = _b[DiD]/`delta_tax'/100
global semi_e_2006_`x'_`y' = semi_e


 		local did_`x'_`y'_B = _b[DiD]
		local se_`x'_`y'_B = _se[DiD]
		local pval = 2*(1-normal(abs(_b[DiD]/_se[DiD])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local did_`x'_`y'_B = trim("`: display %5.2f `did_`x'_`y'_B''")
		global DiD_`x'_`y'_B = "`did_`x'_`y'_B'`p'"
		global SE_`x'_`y'_B= trim("`: display %5.2f `se_`x'_`y'_B''")
			drop star
		display "${DiD_`x'_`y'_B}"
		
// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' ==80 { 
	outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(2) replace ///
	sortvar(DiD lnnet_avgtt treat_i i.year ) ctitle(" ", " ", "Reduced", "(level)") title("DiD Estimations Residents only, Control Group: `x'-`y'% of Threshold") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80} )			
	}		

	
	
		
*  REDUCED FORM DID REGRESSION IN LOGS 
display _newline(5)
display "Reduced Form (in logs), Control group: `x'-`y'%"

replace Inmovers = ln(Inmovers)

sum Inmovers if year <2006 & year >2000 & treat_i == 1
scalar define lnmeanoutcome = `r(mean)'
global lnmean_incomers`x'_`y' = lnmeanoutcome

reg Inmovers DiD  treat_i  ib2005.year, vce(robust)  

 		local did_`x'_`y'_C = _b[DiD]
		local se_`x'_`y'_C = _se[DiD]
		local pval = 2*(1-normal(abs(_b[DiD]/_se[DiD])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local did_`x'_`y'_C = trim("`: display %5.3f `did_`x'_`y'_C''")
		global DiD_`x'_`y'_C = "`did_`x'_`y'_C'`p'"
		global SE_`x'_`y'_C= trim("`: display %5.3f `se_`x'_`y'_C''")
			drop star

			
// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' == 80 { 
	outreg2  using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(2) append ///
	sortvar(DiD lnnet_avgtt treat_i i.year ) ctitle(" ", " ", "Reduced", "(log)") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80} )
}	




		
* 2SLS - IN LOGS TO OBTAIN THE ELASTICITY
gen lnnet_avgtt = ln(1-avgtt)
label var lnnet_avgtt "$\varepsilon_{\bar{\tau}}$"

	// HAUSMAN TEST (put this first, to include results in regression tables
	* 	Hypothesis H0: tax rates are exogenous
	*	Note:  	1) if all vars are exogenous, both 2sls and ols are consistent
	*			2) if all vars are exogenous, ols is more efficient, but it's inconsistent if violated

	foreach var in lnnet_avgtt  {
		ivreg2 Inmovers treat_i   ib2005.year (`var' = DiD)   
		estimates store iv_`var'

		reg Inmovers `var' treat_i  ib2005.year 
		estimates store ols_`var'

		hausman iv_`var' ols_`var'
		scalar chi07_`x'_`y' = r(chi2)
		scalar p07_`x'_`y' = r(p)
	} 


display _newline(5)
display "IV-Regression, AVERAGE NTR, Control group: `x'-`y'%"

ivreg2 Inmovers treat_i  ib2005.year (lnnet_avgtt = DiD) , first savefirst savefprefix(_1st_`x'_`y'_) redundant(DiD) robust
est sto iv07_`x'_`y'

di e(sstatp)
di e(archi2p)

//statistics to be saved in first-stage table:
display "first stage statistics"
// empty the locals
local Fstat
local Fstat_alt1
local Fstat_alt2
local widstat
local idstat
local idp
local esstat
local estatp
local arf
local arfp
local archi2
local archi2p
local sstat
local sstatp

// save stats as locals with nice display format
local idstat 	= trim("`: display %5.2f e(idstat)'")
local idp 		= trim("`: display %5.3f e(idp)'")
local esstat 	= trim("`: display %5.2f e(estat)'")
local estatp 	= trim("`: display %5.3f e(estatp)'")
local arf 		= trim("`: display %5.2f e(arf)'")
local arfp 		= trim("`: display %5.3f e(arfp)'")
local archi2 	= trim("`: display %5.2f e(archi2)'")
local archi2p	= trim("`: display %5.3f e(archi2p)'")
local sstat 	= trim("`: display %5.2f e(sstat)'")
local sstatp 	= trim("`: display %5.3f e(sstatp)'")
	
// F-Test
local widstat = trim("`: display %5.2f e(widstat)'") // F statistic for weak identification (Cragg-Donald or Kleibergen-Paap)
local Fstat_alt1 = trim("`: display %5.2f e(cdf)'") // should be the same as widstat, but not valid when errors are not iid! (e.g., robust)
local Fstat_alt2 = trim("`: display %5.2f e(F)'")	// overall F-test of 2nd stage
global Ftest_`x'_`y' = `widstat'
global Ftest2_`x'_`y' = `Fstat_alt1'
global Ftest3_`x'_`y' = `Fstat_alt2'

// compute p-value for F-Test widstat
	local df1 = e(ardf)
	local df2 = e(ardf_r)
	local F = e(widstat)

	display "These are the parameters for the F-Test: F( `df1', `df2', `F')"
	local pvalF = Ftail(`df1', `df2', `F')
		display "This is the F-stat pvalue for `x'-`y'% group: `pvalF'"
				gen starF = ""
				replace starF="*" in 1 if `pvalF'<.1 &`pvalF'>=.05 //print star if p-value between
				replace starF="**"  in 1 if `pvalF'<.05 &`pvalF'>=.01
				replace starF="***"  in 1 if `pvalF'<.01
				local pF = starF[1]
					display "Stars for F-stat pvalue for `x'_`y': `pF'"
		global Ftest_`x'_`y'_st = "`widstat'`pF'"
		local Ftest_pval = trim("`: display %5.3f `pvalF''")
		global Ftest_pval_`x'_`y' = `Ftest_pval'
				drop starF

// save coefficients, standard errors and stars for final output table
 		local se_`x'_`y'_E = _se[lnnet_avgtt]
		local e_`x'_`y'_E = _b[lnnet_avgtt]
		local pval = 2*(1-normal(abs(_b[lnnet_avgtt]/_se[lnnet_avgtt])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local e_`x'_`y'_E = trim("`: display %5.3f `e_`x'_`y'_E''")
		global E_`x'_`y'_E = "`e_`x'_`y'_E'`p'"
		global SE_`x'_`y'_E= trim("`: display %5.3f `se_`x'_`y'_E''")
			drop star
			
// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' ==80 { 
	outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.year  ) ctitle(" ", " ", "2SLS", "(log)") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80}, ///
			Hausman$ ^1$ , chi07_60_80, P-value, p07_60_80)
	}
	

estimates restore _1st_`x'_`y'_lnnet_avgtt
 outreg2  using "DiD_Residents_inctreat_first_`x'_`y'_uw.xls",  excel label replace ///
   addstat(	/*"Cragg-Donald F statistic", `Fstat_alt1', "F", `Fstat_alt2',*/ ///
   			"Weak identification F test$^a$", `widstat', "\hspace{0.5cm}\textit{P-value}", `Ftest_pval', ///
            "Underidentification test$^b$", `idstat', "\hspace{0.5cm}\textit{P-value}", `idp' , ///
			 "$\chi^2$ test  significance endog. regressors$^c$", `archi2' , "\hspace{0.5cm}\textit{P-value}", `archi2p' ,  ///
			 "S statistic significance endog. regressors$^d$", `sstat', "\hspace{0.5cm}\textit{P-value}", `sstatp') ///
			 /*"Anderson-Rubin F-test of significance of endogenous regressors", `arf', "\hspace{0.5cm}\textit{P-value}", `arfp',*/ ///
			 /*"Endogeneity test (C statistic or difference-in-Sargan stat)", `esstat', "\hspace{0.5cm}\textit{P-value}", `estatp'*/  ///
        addnote("Robust standard errors in parentheses.") ///
		keep(lnnet_avgtt DiD ib2005.year treat_i ) ctitle("2001-07", "Residents", "`x'-`y'\%")  

	


* OLS MODEL, NO INSTRUMENT FOR TAX RATE
display _newline(5)
display "OLS Model (no IV), avg. NTR, control group: `x'-`y'%"  
reg Inmovers lnnet_avgtt treat_i  ib2005.year, vce(robust)

// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' == 80 { 
	outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.year ) ctitle(" ", " ", "OLS", "(log)") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80})
}

restore

cap rm "DiD_Residents_inctreat_first_`x'_`y'_uw.txt"
cap rm "DiD_Residents_inctreat_`x'_`y'_uw.txt"
cap rm "DiD_Resultstable_Residents_`x'_`y'_uw.txt"
}



*************************************************************************************************

* REGRESSIONS *
// specification with 2 treatments (2006 and 2008)

cd "$mypathRR/Results/Resident/results_01-10"


local b " 60 60"
local t " 80 95"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"


preserve
drop if year>2010


collapse (count) Inmovers=persid (mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(year treat_i)
label var Inmovers "Residents"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var year "Year"

sort treat_i year


gen Period1=0
replace Period1=1 if year>=2006&year<2008
gen DiD1=treat_i*Period1

gen Period2=0
replace Period2=1 if year>=2008
gen DiD2=treat_i*Period2

gen weight=Inmovers

* for the semi-elasticities: average change in tax for treatment group
sort treat_i year

bys treat_i: egen tax_0=mean(avgtt) if year<2006
bys treat_i: egen tax_1=mean(avgtt) if year>2005 & year<2008
bys treat_i: egen tax_2=mean(avgtt) if year>2007 
bys treat_i (year): replace tax_0=tax_0[_n-1] if tax_0==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (year): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (year): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (year): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (year): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (year): replace tax_2=tax_2[_n+1] if tax_2==.

gen delta_tax_06=tax_1-tax_0 if treat_i==1
sum delta_tax_06
local delta_tax_06 = abs(`r(mean)')
local delta_tax_062 = `delta_tax_06'*100

gen delta_tax_08=tax_2-tax_1 if treat_i==1
sum delta_tax_08
local delta_tax_08 = abs(`r(mean)')
local delta_tax_082 = `delta_tax_08'*100

*  Reduced Form 
display _newline(5)
display "Reduced Form, 2 treatments (06 & 08), control group: `x'-`y'%"
reg Inmovers DiD1 DiD2 treat_i  ib2005.year, vce(robust)
scalar define semi_e_06 = _b[DiD1]/`delta_tax_06'/100
scalar define semi_e_08 = _b[DiD2]/`delta_tax_08'/100

 outreg2  using "DiD_Residents_inctreat_`x'_`y'_uw-2tr.xls", excel label  e(F) dec(2) replace ///
  keep(DiD1 DiD2 ib2005.year treat_i ) ctitle("Taxpayers", "Reduced", "(level)") title("DiD Estimations Residents only, Control Group: `x'-`y'% of Threshold") ///
  addnote("Robust standard errors in parentheses.") ///
  addstat($ \Delta \tau_{2006} $ \% pts, `delta_tax_062', Semi-elasticity 2006, semi_e_06, $ \Delta \tau_{2008} $ \% pts, `delta_tax_082', Semi-elasticity 2008, semi_e_08)

replace Inmovers = ln(Inmovers)

display _newline(5)
display "Reduced Form (in logs), 2 treatments (06 & 08), control group: `x'-`y'%" 
reg Inmovers DiD1 DiD2 treat_i  ib2005.year, vce(robust)   
 outreg2  using "DiD_Residents_inctreat_`x'_`y'_uw-2tr.xls", excel label  e(F) dec(3) append ///
  addnote("Robust standard errors in parentheses.") ///
  keep(DiD1 DiD2  ib2005.year treat_i ) ctitle(" ", "Reduced", "(log)") 


* 2SLS
gen lnnet_avgtt = ln(1-avgtt)
label var lnnet_avgtt "$\varepsilon_{\bar{\tau}}$"

	// HAUSMAN TEST (put this first, to include results in regression tables
	* 	Hypothesis H0: tax rates are exogenous
	*	Note:  	1) if all vars are exogenous, both 2sls and ols are consistent
	*			2) if all vars are exogenous, ols is more efficient, but it's inconsistent if violated

	foreach var in lnnet_avgtt {
		ivreg2 Inmovers treat_i   ib2005.year (`var' = DiD1) 
		estimates store iv_`var'

		reg Inmovers `var' treat_i ib2005.year
		estimates store ols_`var'

		hausman iv_`var' ols_`var'
		scalar chi_`var' = r(chi2)
		scalar p_`var' = r(p)
	} 

		foreach var in lnnet_avgtt {
		display `chi_`var''
		}

		foreach var in lnnet_avgtt {
		display `p_`var''
		}


display _newline(5)
display "IV-Regression, 2 treatments, avg. NTR, control group: `x'-`y'%" 

ivreg2 Inmovers treat_i  ib2005.year (lnnet_avgtt = DiD1 DiD2) , first savefirst savefprefix(_1st2tr_`x'_`y'_) redundant(DiD1 DiD2) robust  
est store ivreg_avgttr_`x'_`y'

di e(sstatp)
di e(archi2p)
matlist e(first)

//statistics to be saved in first-stage table:
display "first stage statistics"
// empty the locals
local Fstat
local Fstat_alt1
local Fstat_alt2
local widstat
local idstat
local idp
local esstat
local estatp
local arf
local arfp
local archi2
local archi2p
local sstat
local sstatp

// save stats as locals with nice display format
local idstat 	= trim("`: display %5.2f e(idstat)'")
local idp 		= trim("`: display %5.3f e(idp)'")
local esstat 	= trim("`: display %5.2f e(estat)'")
local estatp 	= trim("`: display %5.3f e(estatp)'")
local arf 		= trim("`: display %5.2f e(arf)'")
local arfp 		= trim("`: display %5.3f e(arfp)'")
local archi2 	= trim("`: display %5.2f e(archi2)'")
local archi2p	= trim("`: display %5.3f e(archi2p)'")
local sstat 	= trim("`: display %5.2f e(sstat)'")
local sstatp 	= trim("`: display %5.3f e(sstatp)'")
	
// F-Test
local widstat = trim("`: display %5.2f e(widstat)'") // F statistic for weak identification (Cragg-Donald or Kleibergen-Paap)
local Fstat_alt1 = trim("`: display %5.2f e(cdf)'") // should be the same as widstat, but not valid when errors are not iid! (e.g., robust)
local Fstat_alt2 = trim("`: display %5.2f e(F)'")	// overall F-test of 2nd stage?
global Ftest_`x'_`y'_2tr = `widstat'
global Ftest2_`x'_`y'_2tr = `Fstat_alt1'
global Ftest3_`x'_`y'_2tr = `Fstat_alt2'

// compute p-value for F-Test widstat
	local df1 = e(ardf)
	local df2 = e(ardf_r)
	local F = e(widstat)

	display "These are the parameters for the F-Test: F( `df1', `df2', `F')"
	local pvalF = Ftail(`df1', `df2', `F')
		display "This is the F-stat pvalue for `x'-`y'% group: `pvalF'"
				gen starF = ""
				replace starF="*" in 1 if `pvalF'<.1 &`pvalF'>=.05 //print star if p-value between
				replace starF="**"  in 1 if `pvalF'<.05 &`pvalF'>=.01
				replace starF="***"  in 1 if `pvalF'<.01
				local pF = starF[1]
					display "Stars for F-stat pvalue for `x'_`y': `pF'"
					display "STEP 1"
global Ftest_`x'_`y'_st_2tr = "`widstat'`pF'"
					display "STEP 2"
local Ftest_pval = trim("`: display %5.3f `pvalF''")
					display "STEP 3"
		global Ftest_pval_`x'_`y'_2tr = `Ftest_pval'
					display "STEP 4"
				drop starF
					display "STEP 5"

// REGRESSION TABLE FOR APPENDIX
  if `x' == 60 & `y' == 80 {
	outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.year  ) ctitle(" ", " ", "2SLS", "(log)") 
	
	/*///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80}, ///
			$ \Delta \tau_{2008} $ \% pts, ${delta_tax_pct_2008_60_80}, ///
			Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt)*/
  }
  
  if `x' == 60 & `y' == 95 {
  	est restore iv07_`x'_`y'
	outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.year  ) ctitle(" ", "60-95 ", "2SLS", "(log)")  
	
	est restore ivreg_avgttr_`x'_`y'
	outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.year  ) ctitle(" ", "60-95 ", "2SLS", "(log)")  

  }
  
// save coefficients, standard errors and stars for final output table
  		local se_`x'_`y'_E = _se[lnnet_avgtt]
		local e_`x'_`y'_E = _b[lnnet_avgtt]
		local pval = 2*(1-normal(abs(_b[lnnet_avgtt]/_se[lnnet_avgtt])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local e_`x'_`y'_E = trim("`: display %5.3f `e_`x'_`y'_E''")
		global E10_`x'_`y'_E = "`e_`x'_`y'_E'`p'"
		global SE10_`x'_`y'_E= trim("`: display %5.3f `se_`x'_`y'_E''")
			drop star
	

 
estimates restore _1st2tr_`x'_`y'_lnnet_avgtt
outreg2  using "DiD_Residents_inctreat_first_`x'_`y'_uw-2tr.xls",  excel label replace  ///
   addstat(	/*"Cragg-Donald F statistic", `Fstat_alt1', "F", `Fstat_alt2',*/ ///
   			"Weak identification F test$^a$", `widstat', "\hspace{0.5cm}\textit{P-value}", `Ftest_pval', ///
            "Underidentification test$^b$", `idstat', "\hspace{0.5cm}\textit{P-value}", `idp' , ///
			 "$\chi^2$ test  significance endog. regressors$^c$", `archi2' , "\hspace{0.5cm}\textit{P-value}", `archi2p' ,  ///
			 "S statistic significance endog. regressors$^d$", `sstat', "\hspace{0.5cm}\textit{P-value}", `sstatp') ///
			 /*"Anderson-Rubin F-test of significance of endogenous regressors", `arf', "\hspace{0.5cm}\textit{P-value}", `arfp',*/ ///
			 /*"Endogeneity test (C statistic or difference-in-Sargan stat)", `esstat', "\hspace{0.5cm}\textit{P-value}", `estatp'*/  ///
			 addnote("Robust standard errors in parentheses.") ///
	keep(lnnet_avgtt DiD1 DiD2 ib2005.year treat_i ) ctitle("2001-10", "Residents", "`x'-`y'\%")  


* ols model, no IV
display _newline(5)
display "OLS, no IV, avg. NTR, control group: `x'-`y'%"
reg Inmovers lnnet_avgtt treat_i  ib2005.year, vce(robust) 
 outreg2  using "DiD_Residents_inctreat_`x'_`y'_uw-2tr.xls", excel label  e(F)  dec(3)  append ///
 addnote("Robust standard errors in parentheses.") ///
 keep(lnnet_avgtt ib2005.year treat_i ) ctitle(" ", "OLS", "(log)") 

// REGRESSION TABLE FOR APPENDIX
 if `x' == 60 & `y' == 80 {
  outreg2 using "$mypathRR/Results/appendixtables/B3_appendixtable.xls", excel label  e(F) dec(3) append ///
 sortvar(DiD lnnet_avgtt treat_i i.year  ) ctitle(" ", " ", "OLS", "(log)") 
 /* ///
 addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80}, ///
			$ \Delta \tau_{2008} $ \% pts, ${delta_tax_pct_2008_60_80}) */
 }
			
 
est restore ivreg_avgttr_`x'_`y'
 outreg2 using "DiD_Resultstable_Residents_`x'_`y'_uw.xls", excel label dec(3)  append ///
 keep(lnnet_avgtt ib2005.year treat_i ) sortvar(DiD lnnet_mtr lnnet_avgtt treat_i ib2005.year) ctitle(" ", "2SLS", "(log)")  ///
  addnote("Robust standard errors in parentheses.") ///
  addstat(Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt )
 
restore
cap rm "DiD_Residents_inctreat_first_`x'_`y'_uw-2tr.txt"
cap rm "DiD_Resultstable_Residents_`x'_`y'_uw.txt"
cap rm "DiD_Residents_inctreat_`x'_`y'_uw-2tr.txt"
}


